import { createSlice, PayloadAction } from "@reduxjs/toolkit";
import produce from "immer";

export type PageInfoStateType = {
  title: string;
  isPrompt: boolean;
  isPublished: boolean;
  desc?: string;
};

const INIT_STATE: PageInfoStateType = {
  title: "",
  isPrompt: false,
  isPublished: false,
  desc: "",
};

const pageInfoSlice = createSlice({
  name: "pageInfo",
  initialState: INIT_STATE,
  reducers: {
    resetPageInfo: (
      state: PageInfoStateType,
      action: PayloadAction<PageInfoStateType>
    ) => action.payload,

    changePageTitle: produce(
      (draft: PageInfoStateType, action: PayloadAction<string>) => {
        draft.title = action.payload;
      }
    ),
  },
});

export const { resetPageInfo, changePageTitle } = pageInfoSlice.actions;

export default pageInfoSlice.reducer;
